package com.censhine.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.censhine.commons.result.PageInfo;
import com.censhine.commons.result.Tree;
import com.censhine.mapper.ArticleMapper;
import com.censhine.model.Article;
import com.censhine.model.Role;
import com.censhine.service.IArticleService;

/**
 *
 * Organization 表数据服务层接口实现类
 *
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    
    @Override
    public List<Tree> selectTree() {
        List<Article> organizationList = selectTreeGrid();

        List<Tree> trees = new ArrayList<Tree>();
        if (organizationList != null) {
            for (Article organization : organizationList) {
                Tree tree = new Tree();
                tree.setId(organization.getId());
                tree.setText(organization.getTitle()); 
                trees.add(tree);
            }
        }
        return trees;
    }
    
    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        Page<Article> page = new Page<Article>(pageInfo.getNowpage(), pageInfo.getSize());
        
        EntityWrapper<Article> wrapper = new EntityWrapper<Article>();
        wrapper.orderBy(pageInfo.getSort(), pageInfo.getOrder().equalsIgnoreCase("ASC"));
        selectPage(page, wrapper);
        
        pageInfo.setRows(page.getRecords());
        pageInfo.setTotal(page.getTotal());
    }

    @Override
    public List<Article> selectTreeGrid() {
        EntityWrapper<Article> wrapper = new EntityWrapper<Article>();
        wrapper.orderBy("add_time");
        return articleMapper.selectList(wrapper);
    }


}